
package msu.ml.util;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import java.io.*;

public class XmlUtility
{
    public static Document parseXmlFile(String file)
    {
        return parseXmlFile(new File(file));
    }

    public static Document parseXmlFile(File file)
    {
        try
        {
            DocumentBuilderFactory domFactory = 
                DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true);
            DocumentBuilder builder = 
                domFactory.newDocumentBuilder();
            return builder.parse(file);
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        return null;
    }

    public static Document parseXmlString(String xml)
    {
        try
        {
            DocumentBuilderFactory domFactory = 
                DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true);
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            return builder.parse(new ByteArrayInputStream(
                xml.getBytes("UTF-16")));
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        return null;
    }

    public static Document parseUri(String uri)
    {
       try
       {
          DocumentBuilderFactory domFactory = 
             DocumentBuilderFactory.newInstance();
          domFactory.setNamespaceAware(true);
          DocumentBuilder builder = 
             domFactory.newDocumentBuilder();
          return builder.parse(uri);
       }
       catch(Exception e)
       {
          System.out.println(e.getMessage());
       }
       return null;
    }

    public static NodeList xPathQuery(String file, String query)
    {
        try
        {
            DocumentBuilderFactory domFactory = 
                DocumentBuilderFactory.newInstance();
            domFactory.setNamespaceAware(true);
            DocumentBuilder builder = domFactory.newDocumentBuilder();
            Document document = builder.parse(file);
            return xPathQuery(document, query);
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        return null;
    }

    public static NodeList xPathQuery(Document document, String query)
    {
        try
        {
            XPathFactory factory = XPathFactory.newInstance();
            XPath xpath = factory.newXPath();
            XPathExpression expr = xpath.compile(query);
            return (NodeList)(expr.evaluate(document, XPathConstants.NODESET));
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        return null;
    }

    public static Element getSingleElement(Element e, String name)
    {
      return (Element)getSingleNode(e, name);
    }

    public static Node getSingleNode(Element e, String name)
    {
      NodeList list = e.getElementsByTagName(name);
      return (list.getLength() > 0) ? list.item(0) : null;
    }
}
